home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / archiver / ltarv3.zip / TARV.C < prev    next >
C/C++ Source or Header  |  1992-01-28  |  5KB  |  270 lines

  1. #include <conio.h>
  2. #include <dir.h>
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. char defdir[80], *sourDir, command[128], *comd ;
  9. char dos[80], unix[256] ;
  10.  
  11. long *direct ;
  12. size_t totalDir ;
  13.  
  14. FILE *mapFile ;
  15.  
  16. const ScrLeng = 20 ;
  17.  
  18. int isDir( char *s ) {
  19.  
  20.     char x = s[strlen(s) - 1] ;
  21.  
  22.     return x == '/' || x == '\\' ;
  23. }
  24.  
  25. void filname( size_t p ) {
  26.  
  27.     if ( p < totalDir ) {
  28.         fseek( mapFile, *( direct + p ), SEEK_SET ) ;
  29.         fscanf( mapFile, "%256s %80s\n", unix, dos ) ;
  30.     }
  31. }
  32.  
  33. void procesFile( void ) {
  34.  
  35.     size_t i = 0 ;
  36.     long pos ;
  37.  
  38.     rewind( mapFile ) ;
  39.     while ( !feof( mapFile ) ) {
  40.         fscanf( mapFile, "%s %s\n", unix, dos ) ;
  41.         if ( !isDir( unix ) )
  42.             i++ ;
  43.     }
  44.     totalDir = i ;
  45.     direct = ( long * )malloc( i * sizeof ( long ) ) ;
  46.     if ( direct == NULL ) {
  47.         fputs( "Too many file entries.\n", stderr ) ;
  48.         exit( 3 ) ;
  49.     }
  50.     rewind( mapFile ) ;
  51.     i = 0 ;
  52.     while ( !feof( mapFile ) ) {
  53.         pos = ftell( mapFile ) ;
  54.         fscanf( mapFile, "%s %s\n", unix, dos ) ;
  55.         if ( !isDir( unix ) ) {
  56.             *( direct + i ) = pos ;
  57.             i++ ;
  58.         }
  59.     }
  60. }
  61.  
  62. char *cmd = "TAPV: [Enter]Apply, [NumPad]Scroll [Ins]ReComd [Esc]Bye" ;
  63.  
  64. void printScrn( void ) {
  65.  
  66.     gotoxy( 1, 25 ) ;
  67.     cputs( cmd ) ;
  68.     clreol() ;
  69.     gotoxy( 70, 25 ) ;
  70.     cprintf( "[%-8s]", comd ) ;
  71.     clreol() ;
  72. }
  73.  
  74. char *question( char *ques ) {
  75.  
  76.     static char ans[83], *r ;
  77.  
  78.     ans[0] = 81 ;
  79.     gotoxy( 1, 25 ) ;
  80.     clreol() ;
  81.     gotoxy( 1, 25 ) ;
  82.     cprintf( ques ) ;
  83.     r = cgets( ans ) ;
  84.     return r ;
  85. }
  86.  
  87. void displayFile( size_t pos ) {
  88.  
  89.     int i ;
  90.  
  91.     for ( i = 0 ; i < ScrLeng ; i++ ) {
  92.         gotoxy( 1, i + 1 ) ;
  93.         if ( pos + i + 1 > totalDir )
  94.             clreol() ;
  95.         else {
  96.             filname( pos + i ) ;
  97.             cprintf( "%5d %-70s", pos + i + 1, unix ) ;
  98.         }
  99.     }
  100. }
  101.  
  102. void applyCommand( void ) {
  103.  
  104.     static char command[160], *ptr, name[80] ;
  105.  
  106.     clrscr() ;
  107.     if ( comd ) {
  108.         sprintf( name, "%s\\%s", sourDir, dos ) ;
  109.         if ( ( ptr = strstr( comd, "$s" ) ) != NULL ) {
  110.             *ptr = '%' ;
  111.             sprintf( command, comd, name ) ;
  112.             *ptr = '$' ;
  113.         } else
  114.             sprintf( command, "%s %s", comd, name ) ;
  115.         system( command ) ;
  116.     }
  117.     gotoxy( 1, 25 ) ;
  118.     cputs( "Press any key to continue..." ) ;
  119.     clreol() ;
  120.     getch() ;
  121.     clrscr() ;
  122. }
  123.  
  124. void drawBar( size_t pos, size_t h ) {
  125.  
  126.     filname( pos ) ;
  127.     gotoxy( 1, 21 ) ;
  128.     cprintf( "%s\\%s", sourDir, dos ) ;
  129.     clreol() ;
  130.     gotoxy( 1, 22 ) ;
  131.     cputs( unix ) ;
  132.     clreol() ;
  133.     gotoxy( 1, pos - h + 1 ) ;
  134. }
  135.  
  136. void shell( void ) {
  137.  
  138.     size_t curPos = 0, headPos = 0 ;
  139.     int done = 0, disp = 1 ;
  140.  
  141.     clrscr() ;
  142.     printScrn() ;
  143.     procesFile() ;
  144.     while ( !done ) {
  145.         if ( disp ) {
  146.             displayFile( headPos ) ;
  147.             disp = 0 ;
  148.         }
  149.         drawBar( curPos, headPos ) ;
  150.         switch ( getch() ) {
  151.             case 0x1b :
  152.                 done = 1 ;
  153.                 break ;
  154.             case '\r' :
  155.                 applyCommand() ;
  156.                 disp = 1 ;
  157.                 printScrn() ;
  158.                 break ;
  159.             case 0 :
  160.             switch( getch() ) {
  161. /* up */        case 0x48 :
  162.                 if ( curPos == 0 ) {
  163.                     curPos = totalDir - 1 ;
  164.                     if ( totalDir > ScrLeng )
  165.                         headPos = totalDir - ScrLeng ;
  166.                     disp = 1 ;
  167.                 } else {
  168.                     curPos-- ;
  169.                     if ( curPos < headPos ) {
  170.                         headPos-- ;
  171.                         disp = 1 ;
  172.                     }
  173.                 }
  174.                 break ;
  175. /* down */        case 0x50 :
  176.                 if ( curPos == totalDir - 1 ) {
  177.                     curPos = 0 ;
  178.                     headPos = 0 ;
  179.                     disp = 1 ;
  180.                 } else {
  181.                     curPos++ ;
  182.                     if ( curPos >= headPos + ScrLeng ) {
  183.                         headPos++ ;
  184.                         disp = 1 ;
  185.                     }
  186.                 }
  187.                 break ;
  188. /* PgUp */        case 0x49 :
  189.                 if ( curPos >= ScrLeng ) {
  190.                     disp = 1 ;
  191.                     headPos = headPos < ScrLeng ?
  192.                         0 : headPos - ScrLeng ;
  193.                     curPos -= ScrLeng ;
  194.                 }
  195.                 break ;
  196. /* PgDn */        case 0x51 :
  197.                 if ( totalDir < ScrLeng )
  198.                     break ;
  199.                 if ( curPos + ScrLeng < totalDir ) {
  200.                     disp = 1 ;
  201.                     headPos += ScrLeng ;
  202.                     if ( headPos + ScrLeng > totalDir )
  203.                         headPos = totalDir - ScrLeng ;
  204.                     curPos += ScrLeng ;
  205.                 } else if ( headPos != totalDir - ScrLeng ) {
  206.                     disp = 1 ;
  207.                     headPos = totalDir - ScrLeng ;
  208.                 }
  209.                 break ;
  210. /* Home */        case 0x47 :
  211.                 if ( headPos != 0 ) {
  212.                     headPos = 0 ;
  213.                     disp = 1 ;
  214.                 }
  215.                 curPos = 0 ;
  216.                 break ;
  217. /* End */        case 0x4f :
  218.                 if ( totalDir > ScrLeng )
  219.                     if ( totalDir - ScrLeng != headPos ) {
  220.                         headPos = totalDir - ScrLeng ;
  221.                         disp = 1 ;
  222.                     }
  223.                 curPos = totalDir - 1 ;
  224.                 break ;
  225. /* Ins */        case 0x52 :
  226.                 comd = question(
  227.                     "Enter DOS Commands while applying :"
  228.                 ) ;
  229.                 printScrn() ;
  230.                 break ;
  231.  
  232.             }
  233.  
  234.         }
  235.     }
  236. }
  237.  
  238. char *banner =
  239.     "Tape Archive viewer 1.0\n" ;
  240. char *usage =
  241.     "[Usage]: TARV tar-mapfile source-dir comd\n"
  242.     "tar-mapfile\tmapfile created by LTAR\n"
  243.     "source-dir\tsubdir of archive files\n"
  244.     "comd\t\tcomd you use while applying\n"
  245.     "For example: TARV c:\\mytar.map c:\\mytar type\n" ;
  246.  
  247. void main( int argc, char *argv[] ) {
  248.  
  249.     fputs( banner, stderr ) ;
  250.  
  251.     if ( argc != 4 ) {
  252.         fputs( usage, stderr ) ;
  253.         exit( 1 ) ;
  254.     }
  255.  
  256.     mapFile = fopen( argv[1], "rt" ) ;
  257.     sourDir = argv[2] ;
  258.     comd = argv[3] ;
  259.     if ( mapFile == NULL ) {
  260.         fprintf( stderr, "Error open %s.\n", argv[1] ) ;
  261.         exit( 2 ) ;
  262.     }
  263.  
  264.     getcwd( defdir, 80 ) ;
  265.     shell() ;
  266.     fclose( mapFile ) ;
  267.     setdisk( defdir[0] - 'A' ) ;
  268.     chdir( defdir ) ;
  269.     clrscr() ;
  270. }